function RotationControl(){

	this.selectedModels = []; //models by engine were selected with mouse
	this.polygon = null; // first polygon "onDrag start"
	this.path = []; //rotation vector
	this.level  = [null,null,null]; //rotation level (one of x,y,z here)
	this.groupBy = null; //models participated in rotation have same level coordinat
	
	this.rotationModels = []; 
	
	
	this.addToSelection = function(model){
		this.selectedModels.push(model);
	};
	
	this.getVector = function(){
		var path = this.path;
		var level = this.level;
		var vector = this.groupBy;
		
		var angle = 90;
		if(path[0] < path[path.length-1])
			angle = -90;
		var vectorAxName = '';
		switch(vector){
			case 0: 
				vectorAxName = 'rotateX'; 
				if(level[1] == null && level[2] < 0){
					angle *= -1;
				}
				if(level[2] == null && level[1] > 0){
					angle *= -1;
				}
				break;
			case 1: 
				vectorAxName = 'rotateY'; 
				if(level[2] == null && level[0] < 0){
					angle *= -1;
				}
				if(level[0] == null && level[2] > 0){
					angle *= -1;
				}
				break;
			case 2: 
				vectorAxName = 'rotateZ'; 
				if(level[0] == null && level[1] < 0){
					angle *= -1;
				}
				if(level[1] == null && level[0] > 0){
					angle *= -1;
				}
				break;
		}
		//console.log(vectorAxName + ' ' + angle );
		//console.log(level);
		var historyRecord = new HistoryRecord();
		historyRecord.path = path;
		historyRecord.level = level;
		historyRecord.vector = vector;
		historyRecord.fname = vectorAxName;
		historyRecord.angle = angle;
		//return {path: path, level: level, vector:vector, fname: vectorAxName, angle:angle };
		return historyRecord;
	};
	
	this.getSelectedModels = function(){
		this.selectedModels = getUniqueArray(this.selectedModels);
		return this.selectedModels;
	};
	
	this.setInitialPolygon = function(obj){
		this.polygon = obj;
	};
	
	this.calculateVector = function(){
		this.selectedModels = getUniqueArray(this.selectedModels); //save time to reduce number of models to minimum
		if (this.selectedModels.length <= 1){
			return false;
		}
		var r = this.selectedModels[0].initialCenter; 				//reference point in first 3d model to start with 
		
		//compare set of models (its center point) to reference point
		for(var i=1; i<this.selectedModels.length; i++){
			var c = this.selectedModels[i].initialCenter;
			for(var k=0; k<3; k++){
				if(c[k] == r[k]){
					//populate level
					this.level[k] = c[k]; 
				}else{
					//populate path vector
					this.path.push(c[k]);
				}
			}
		}
		//get first model data to complete vector path
		var fixed = [];
		for(var k=0; k<3; k++){
			if(this.level[k] == null){
				fixed.push(r[k]);
				this.path = fixed.concat(this.path);
				break;
			}
		}
		//at this point we have rotation vector and 2 possible levels 
		//get z_average for first selected polygon and compare to level array to exclude one level
		var average = [0,0,0];
		for(var i=0; i < this.polygon.points.length; i++){
			average[0] += this.polygon.points[i][0];
			average[1] += this.polygon.points[i][1];
			average[2] += this.polygon.points[i][2];
		}
		for(var i=0; i < average.length; i++){
			average[i] = average[i] / this.polygon.points.length;
		}
		
		for(var i=0; i < average.length; i++){
			if(average[i] == this.selectedModels[0].initialCenter[i] && average[i]== this.level[i]){
				this.groupBy = i;
				break;
			}
		}
		return true;
	};
	
	this.resetSelection = function(){
		this.selectedModels = [];
		this.polygon = null;
		this.path = [];
		this.level  = [null,null,null];
		this.groupBy = null;
		
	};
	
	
	function getUniqueArray(arr) {
		var a = [];
		var l = arr.length;
		for(var i=0; i<l; i++) {
		  for(var j=i+1; j<l; j++) {
			if (arr[i] === arr[j])
			  j = ++i;
		  }
		  a.push(arr[i]);
		}
		return a;
	}
}

var Rotation = new RotationControl();